<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>龟兔赛跑</title>
    <style>
        #box1{
            width: 100px;
            height: 100px;
            background: url("../imagconman/tortoise.gif") no-repeat;
            background-size: 100px;
            position: absolute;
            left: 0;
        }
        #box2{
            width: 100px;
            height: 100px;
            background-image: url("../imagconman/rabbit.gif");
            background-size: 100px;
            position: absolute;
            left: 0;
            top: 150px;
        }
        #box5{
            width: 100px;
            height: 100px;
            background-color: green;
            position: absolute;
            left: 0;
            top: 300px;
        }
    </style>
    <script>
        window.onload=function () {
            var btn01=document.getElementById("btn01");
            var btn02=document.getElementById("btn02");
            var btn03=document.getElementById("btn03");
            var btn04=document.getElementById("btn04");
            var btn06=document.getElementById("btn06");
            var box1=document.getElementById("box1");
            var box2=document.getElementById("box2");
            var box5=document.getElementById("box5");
            btn01.onclick=function () {
                move(box1,"left",900,10);
            };
            btn02.onclick=function () {
                move(box2,"left",900,10);
            };
            btn03.onclick=function () {
            move(box1,"left",0,10);
            };
            btn04.onclick=function(){
                move(box2,"left",0,10);
            };
            btn06.onclick=function(){
                move(box5,"width",900,10,function () {
                    move(box5,"height",300,10,function () {
                        move(box5,"top",100,10,function () {
                            move(box5,"top",300,10,function () {
                                move(box5,"width",100,10,function () {
                                    move(box5,"height",100,10,function () {

                                    });
                                });
                            });
                        });
                    });
                });
            };
        };
        /* 参数  1.obj 要执行的对象
       *         2.attr 要执行动画的样式比如width left top height
      *         3.target 目标
      *        4.speed  移动的速度（正值 向右移动 ;负值 向左移动）
      *         5.callback 回调函数 会将在我们执行完动画之后执行
      * */

        function move(obj,attr,target,speed,callback) {
            //判断speed的正负值 如果从0-800则speed为正 ;如果800-0 则speed为负值
            var current=parseInt(getStyle(obj,attr));
            if (current>target){
                speed=-speed;
            }
            //点击按钮以后box1向右移动
            clearInterval(obj.timer);
            obj.timer=setInterval(function () {
                var oldValue=parseInt(getStyle(obj,attr));
                var newValue=oldValue+speed;
                //向右 移动0-800 需要判断newValue是否大于target
                //向左 移动800-0 需要判断newValue是否小于target
                if ((speed>0&&newValue>target) ||(speed<0&&newValue<target)){
                    newValue=target;
                }
                obj.style[attr]=newValue+"px";
                if (newValue==target){
                    clearInterval(obj.timer);
                    callback&&callback();
                }
            },50);
        }
        function getStyle(obj,name){
            // if (window.getComputedStyle){
            //     return getComputedStyle(obj,null)[name];//正常浏览器的方式
            // }else {
            //     return obj.currentStyle[name];//IE
            // }
            return window.getComputedStyle?getComputedStyle(obj,null)[name]:obj.currentStyle[name];

        }
    </script>
</head>
<body>
    <button id="btn01">乌龟开跑点</button>
    <button id="btn02">兔子开跑点</button>
    <button id="btn03">乌龟终点</button>
    <button id="btn04">兔子终点</button>
    <button id="btn06">div测试</button>
    <br /><br /><br /><br />
    <div id="box1"></div>
    <div id="box2"></div>
    <div id="box3" style="width: 0;height: 1000px;position:absolute;border-left: 1px black solid;left: 900px;top:0" l></div>
    <div id="box5"></div>
</body>
</html>